var request = require("../../../utils/request.js");
var util = require('../../../utils/util.js');
// pages/index/index.js
// 获取底部自定义菜单

const app = getApp()
Page({
  /**
   * 页面的初始数据
   */

  data: {
    flag: app.globalData.scopeu, //登陆授权页面
    swiperArr: [],
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    current: 0,
    hotCourse: [],
    latestCourse:[],
    skillsCourse:[],
    moreselect: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // app.editTabBar();
    let scopeu = app.globalData.scopeu;

    console.log(scopeu)
    //未授权
    if (scopeu == false) {
      wx.hideTabBar({

      });

      this.setData({
        flag: true
      })
    }
    else {
      wx.showTabBar({

      })
      this.setData({
        scopeu: false
      })
    }
  },
 
  //跳转普通课程
  detail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/course/detail?id='+id,
    })
  },

  //处理点击授权登陆
  loginInfo: function () {
    var that = this;
    
    app.checkUserInfo();
    // 用户授权返回token_id 不为空视为登陆成功
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.init();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  init() {
    var that = this;

    // 获取banner图
    request.request("api/Course/GetBanner").then(function(res){
      console.log(res);
      var result = res.data;
      if (result.status == 1) {
        
        for (var i = 0; i < result.data.length; i++) {
          var pic = result.data[i]['Pic'].split(',')

          for (var j in pic) {
            pic[j] = "http://qbonbon.qclub-online.com/_Document/Image/" + pic[j]

          }

          result.data[i]['Pic'] = pic[0];

        }

        that.setData({
          "swiperArr": result.data
        });
        
      }
    });

    // 人气课程
    request.request("api/Course/GetCoursesByHot", { pageIndex: 1, pageSize:3}).then(function(res){
      var result = res.data;
      
      if (result.status == 1){

        for (var i = 0; i < result.data.length; i++) {
          var pic = result.data[i]['Pic'].split(',')

          for (var j in pic) {
            pic[j] = "http://qbonbon.qclub-online.com/_Document/Image/" + pic[j]

          }

          result.data[i]['Pic'] = pic[0];
        
        }

        that.setData({
          "hotCourse": result.data
        })

        console.log(that.data.hotCourse)
      }
    });

    //最新上线
    request.request("api/Course/GetCoursesByOnlineTimeNear", { pageIndex: 1, pageSize: 3 }).then(function (res) {
      var result = res.data;
    
      if (result.status == 1) {

        for (var i = 0; i < result.data.length; i++) {
          var pic = result.data[i]['Pic'].split(',')

          for (var j in pic) {
            pic[j] = "http://qbonbon.qclub-online.com/_Document/Image/" + pic[j]

          }



          result.data[i]['Pic'] = pic[0];
          result.data[i]['slider'] = pic;
        }

        console.log("处理结果集：", result.data);

        that.setData({
          "latestCourse": result.data
        })
      }
    });

    //实用技巧
    request.request("api/Course/GetCoursesByTag", { id:71, pageIndex: 1, pageSize: 3 }).then(function (res) {
      var result = res.data;

      if (result.status == 1) {

        for (var i = 0; i < result.data.length; i++) {
          var pic = result.data[i]['Pic'].split(',')

          for (var j in pic) {
            pic[j] = "http://qbonbon.qclub-online.com/_Document/Image/" + pic[j]

          }



          result.data[i]['Pic'] = pic[0];
          result.data[i]['slider'] = pic;
        }

        console.log("处理结果集：", result.data);

        that.setData({
          "skillsCourse": result.data
        })
      }
    });


  },
  imageError: function (e) {
    var that = this;
    
    util.errImgFun(e, that);
  },

  // 打开分类列表
  openView:function(e){
    var id = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/course/categoryList?id='+id+"&type="+type
    })
  },

  //打开其他页面
  openPage:function(e){
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: "/pages/"+url,
    })
  },

  //打开搜索页面
  search:function(){
    wx.navigateTo({
      url: "/pages/index/search/index"
    })
  }



})